package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.message.MessageTemplate;
import com.cy.ppj.model.dto.message.CommissionMessageDTO;
import com.cy.ppj.model.dto.message.UserCommissionRecordDTO;
import com.cy.ppj.model.dto.wechat.WecahtTemplateDataDTO;
import com.cy.ppj.model.dto.wechat.WechatTemplateDTO;
import com.cy.ppj.model.enums.CommissionType;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.enums.wechat.WechatTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description: 佣金消息
 * @author: Ocean
 * @create: 2018-08-09 19:15
 **/
@Slf4j
@Message(type = MessageType.PAY_COMMISSION)
@Service
public class CommissionMessageImpl extends MessageServiceImpl {

    @Override
    public void execute(String message) {

        Long orderId = Long.valueOf(message);
        CommissionMessageDTO template = messageTemplateMapper.selectCommissionMessageTemplate(orderId);
        template.setOrderId(orderId);
        sendWechatTemplate(template);
        sendMessage(template);
    }

    private void sendMessage(CommissionMessageDTO template) {

        MessageTemplate messageTemplate = messageTemplateService.queryMessageTemplateByTypeInCache(MessageType.PAY_COMMISSION);
        if (messageTemplate == null) {
            log.info("订单[{}]发放佣金,没有消息模版。", template.getOrderId());
            return;
        }

        if (!messageTemplate.getOpen()) {
            log.info("订单[{}]发放佣金,消息模版未开启。", template.getOrderId());
            return;
        }

        Date time = new Date();
        List<MessageRecord> messageRecords = new ArrayList<>();
        template.getRecords().stream().filter(r -> !r.getType().equals(CommissionType.BORKER_COMPANY)).forEach(r -> {

            Map<String, Object> map = new HashMap<>();
            map.put("amount", r.getAmount());
            map.put("type", r.getType().getText());
            map.put("createTime", CommonUtils.dateFormat(r.getCreateTime()));
            map.put("remark", r.getRemark());
            String content = SpringELUtils.getValue(messageTemplate.getContent(), map);

            MessageRecord messageRecord = new MessageRecord();
            messageRecord.setType(MessageType.PAY_COMMISSION);
            messageRecord.setContent(content);
            messageRecord.setUserId(r.getUserId());
            messageRecord.setCreateTime(time);
            messageRecord.setUrl("");
            messageRecords.add(messageRecord);
        });

        messageRecordService.addMessageRecords(messageRecords);
    }

    /**
     * 发送微信模版
     *
     * @param template
     */
    private void sendWechatTemplate(CommissionMessageDTO template) {

        if (template.getOpenId() == null) {
            log.info("订单[{}]发放佣金,用户没有绑定微信,无法发送微信消息。", template.getOrderId());
            return;
        }

        Optional<UserCommissionRecordDTO> optional = template.getRecords().stream().filter(t -> t.getUserId().equals(template.getOrderUserId())).findFirst();
        if (optional == null) {
            return;
        }

        UserCommissionRecordDTO userCommissionRecord = optional.get();

        WechatTemplateDTO send = new WechatTemplateDTO();
        send.setOpneId(template.getOpenId());
        send.setAccessToken(wechatService.getAccessToken());
        send.setTemplateId(WechatTemplate.PAY_COMMISSION.getTemplateId());

        WecahtTemplateDataDTO first = new WecahtTemplateDataDTO();
        first.setValue("您获得了一笔佣金");

        WecahtTemplateDataDTO keyword1 = new WecahtTemplateDataDTO();
        keyword1.setValue(userCommissionRecord.getAmount().toString().concat("元"));

        WecahtTemplateDataDTO keyword2 = new WecahtTemplateDataDTO();
        keyword2.setValue(CommonUtils.dateFormat(userCommissionRecord.getCreateTime()));

        WecahtTemplateDataDTO remark = new WecahtTemplateDataDTO();
        remark.setValue("详情可进入匹配家，在我-我的佣金中查看明细");

        send.getData().put("first", first);
        send.getData().put("keyword1", keyword1);
        send.getData().put("keyword2", keyword2);
        send.getData().put("remark", remark);

        wechatService.sendTemplateMessage(send);
    }

}
